﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using FileHelpers;

namespace MechanicalReport.PDFExport
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string filename = ConfigurationManager.AppSettings["csvfilePath"];
            string localFolderPath = ConfigurationManager.AppSettings["localPath"];

            FileHelperEngine engine = new FileHelperEngine(typeof(PDFExport));

            PDFExport[] records = engine.ReadFile(filename) as PDFExport[];
            string url = "http://tamoss01.turners.co.nz/Mechanical%20Reports";
            List<ErrorDetails> errors = new List<ErrorDetails>();
            foreach (PDFExport pdfExport in records)
            {
                string file = pdfExport.Name;
                string filePath = Path.Combine(url, file);
                string localFilePath = Path.Combine(localFolderPath, file);
                try
                {
                    using (WebClient client = new WebClient())
                    {
                        client.Credentials = new NetworkCredential("mossadmin", "Tur$Auction5", "ta-datacentre");
                        client.DownloadFile(filePath, localFilePath);
                    }
                }
                catch (Exception ex)
                {
                    errors.Add(new ErrorDetails(){GoodID = pdfExport.GoodsId, Exception = ex});
                }
            }
            if(errors.Count > 1)
            {
                foreach (ErrorDetails errorDetailse in errors)
                {
                    Console.WriteLine(errorDetailse.GoodID);
                }
            }
        }
    }

    [DelimitedRecord(",")]
    public class PDFExport
    {
        public string GoodsId;

        public string Name;

        public string Modified;
    }

    public class ErrorDetails
    {
        public string GoodID { get; set; }
        public Exception Exception { get; set; }
    }
}
